/* ************************************************************************************* 
 * CREATED BY:          GIAPCM
 * CREATED DATE:        10/02/2009
 * PROJECT:             MC ENT WEBSITE PROJECT
 * PURPOSE:             
 * MODIFIED BY:         None
 * MODIFIED DATE:       None
 * MODIFIED REASON:     None
 * NOTES:               None
 **************************************************************************************/
using System;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Telerik.Web.UI;
using Telerik.Web.UI.Editor.DialogControls;
using MCWEBSITE.BUL;
using MCWEBSITE.ENTITY;

public partial class Modules_WebsiteBackEnd_wucThumbPreview : System.Web.UI.UserControl
{   

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {            
            this.GetNewsThumbParameter();
            if (rulImage.TargetFolder == "")
            {
                this.SetUploadParameters(lblResourceDir.Text);               
            }
        }
    }

    //Get Thumb Parameter from Request Page
    protected void GetNewsThumbParameter()
    {        
        lblResourceDir.Text = (string)Request.QueryString["ResourceDir"];
        lblImageThumbDir.Text = (string)Request.QueryString["ThumbDir"];
    }

    //SET PARAMETERS FOR RAD UPLOAD
    public void SetUploadParameters(string ResourceDirectory)
    {
        rulImage.TargetFolder = ResourceDirectory;
    }    
    
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (rulImage.UploadedFiles.Count > 0)
        {
            // Get recent uploaded thumb
            string UploadedFilePath = "";
            UploadedFile FirstUploadedFile = rulImage.UploadedFiles[0];
            UploadedFilePath = Server.MapPath(lblResourceDir.Text + "/" + FirstUploadedFile.GetName());

            // Delete thumbimg directory
            DirectoryInfo ThumbDirectory = new DirectoryInfo(Server.MapPath(lblImageThumbDir.Text));
            if (ThumbDirectory.Exists)
                foreach (FileInfo file in ThumbDirectory.GetFiles())
                    file.Delete();
            // Resize to all specific size based on key setting and save to thumbdirectory        
            string[] ThumbSetting = System.Web.Configuration.WebConfigurationManager.AppSettings["Thumbsize"].Split(';');
            FileInfo FileUploaded = new FileInfo(UploadedFilePath);
            if (FileUploaded.Exists)
            {
                for (int i = 0; i < ThumbSetting.Length; i++)
                {
                    string x = ThumbSetting[i].Split('x')[0];
                    string y = ThumbSetting[i].Split('x')[1];
                    ImageHelperExt.MAX_THUMBNAIL_WIDTH = Convert.ToInt32(x);
                    ImageHelperExt.MAX_THUMBNAIL_HEIGHT = Convert.ToInt32(y);
                    ImageHelperExt.CreateThumb(UploadedFilePath, Server.MapPath(lblImageThumbDir.Text) + "/thumb" + ThumbSetting[i] + FirstUploadedFile.GetExtension());
                    // Utilities.ResizeNewImageBestFit(UploadedFilePath, Server.MapPath(lblImageThumbDir.Text), "thumb" + ThumbSetting[i] + FirstUploadedFile.GetExtension(), Convert.ToInt32(x), Convert.ToInt32(y));
                }
            }
        }
    }
}
